package net.sudot.chess.business.dao;

import net.sudot.chess.business.model.WechatGroup;
import org.springframework.stereotype.Repository;

import javax.persistence.TypedQuery;

/**
 * 微信群名和游戏类别关系映射
 *
 * @author zhanyunmeng on 2018-07-14.
 */
@Repository
public class WechatGroupDao extends BaseDao<WechatGroup> {

    /**
     * 获取微信群信息
     *
     * @param gameId 游戏Id
     * @return 返回微信群信息
     */
    public WechatGroup findUnUseByGame(Long gameId) {
        String hql = "SELECT wechatGroup FROM WechatGroup AS wechatGroup INNER JOIN Game AS game ON wechatGroup.categoryId = game.categoryId WHERE wechatGroup.used=false AND game.id=:gameId";
        TypedQuery<WechatGroup> query = entityManager.createQuery(hql, entityClass);
        query.setParameter("gameId", gameId);
        return uniqueResult(query);
    }

    /**
     * 获取微信群信息
     *
     * @param categoryId 游戏类别Id
     * @return 返回微信群信息
     */
    public WechatGroup findUnUseByCategory(Long categoryId) {
        String hql = "SELECT wechatGroup FROM WechatGroup AS wechatGroup WHERE wechatGroup.used=false AND wechatGroup.categoryId=:categoryId";
        TypedQuery<WechatGroup> query = entityManager.createQuery(hql, entityClass);
        query.setParameter("categoryId", categoryId);
        return uniqueResult(query);
    }
}
